package com.wayne;

import com.wayne.interfaces.ProxyCreator;
import com.wayne.proxys.JDKProxyCreator;
import com.wayne.service.IUserApi;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

    /**
     * 创建jdk动态代理工具类
     * @return
     */
    @Bean
    public ProxyCreator proxyCreator() {
        return new JDKProxyCreator();
    }

    @Bean
    public FactoryBean userApi(ProxyCreator proxyCreator) {
        return new FactoryBean<IUserApi>() {

            /**
             * @return 返回代理对象
             * @throws Exception
             */
            @Override
            public IUserApi getObject() throws Exception {
                return (IUserApi) proxyCreator.createProxy(getObjectType());
            }

            @Override
            public Class<?> getObjectType() {
                return IUserApi.class;
            }
        };
    }
}
